package com.raymond.queue.client.service;

import com.raymond.queue.client.QueueClient;
import com.raymond.queue.client.msg.MessageManages;
import com.raymond.queue.common.msg.request.CreateTopicRequest;
import com.raymond.queue.common.msg.request.ProductionMsgRequest;
import com.raymond.queue.common.msg.response.CreateTopicResponse;
import com.raymond.queue.common.msg.response.ProductionMsgResponse;
import com.raymond.queue.common.utils.SequenceIdUtil;

/**
 * 生成者
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-19 11:39
 */
public class Production {

    private QueueClient queueClient;


    public Production(QueueClient queueClient) {
        this.queueClient = queueClient;
    }

    public int put(String topic, byte[] data) {
        ProductionMsgRequest productionMsgRequest = new ProductionMsgRequest(topic,
                data, SequenceIdUtil.getSequenceId());
        MessageManages messageManages = queueClient.syncSend(productionMsgRequest);
        if (messageManages.isDone()) {
            return ((ProductionMsgResponse) messageManages.getResp()).getCode();
        }
        return 1;
    }

    public void asyncPut(String topic, byte[] data, Callback callback) {
        ProductionMsgRequest productionMsgRequest = new ProductionMsgRequest(topic, data, SequenceIdUtil.getSequenceId());
        queueClient.asyncSend(productionMsgRequest, callback);
    }

    public void asyncPackPut(String topic, byte[] data, Callback callback) {
        ProductionMsgRequest productionMsgRequest = new ProductionMsgRequest(topic, data, SequenceIdUtil.getSequenceId());
        queueClient.asyncPackSend(productionMsgRequest, callback);
    }

    public int createTopic(CreateTopicRequest createTopicRequest) {
        createTopicRequest.setSequenceId(SequenceIdUtil.getSequenceId());
        MessageManages messageManages = queueClient.syncSend(createTopicRequest);
        if (messageManages.isDone()) {
            return ((CreateTopicResponse) messageManages.getResp()).getCode();
        }
        return 1;
    }
}
